/*
 * Copyright (C) 2009 Internal Innovation Technology(IIT)
 * 
 */
package com.iitbd.hxcalc;

import java.util.Locale;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

/**
 *
 * @author Prasun Kanti Howlader <prasun@iitbd.com>
 */
public class ConfigDialog extends javax.swing.JDialog {

    private static Logger logger = Logger.getLogger(ConfigDialog.class.getName());
    private static final long serialVersionUID = 1L;
    private HxcalcUI parent = null;

    /**
     * Creates new form ConfigDialog
     */
    public ConfigDialog(HxcalcUI parent,String title) {
        super(parent, title);
        this.parent = (HxcalcUI) parent;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane3 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        chExtended = new javax.swing.JCheckBox();
        cmbLanguage = new javax.swing.JComboBox();
        cmbMantisse = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);

        jPanel1.setBorder(null);
        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 3, 0, 3, 0, 3, 0};
        jPanel1Layout.rowHeights = new int[] {0, 3, 0, 3, 0, 3, 0, 3, 0};
        jPanel1.setLayout(jPanel1Layout);

        jLabel24.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        jLabel24.setText(Resources.getString("Language"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 65;
        gridBagConstraints.ipady = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jLabel24, gridBagConstraints);

        jLabel25.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        jLabel25.setText(Resources.getString("Mantisse"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 65;
        gridBagConstraints.ipady = 13;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jLabel25, gridBagConstraints);

        jLabel26.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        jLabel26.setText(Resources.getString("Engineering Notation"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipady = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(jLabel26, gridBagConstraints);

        chExtended.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(chExtended, gridBagConstraints);

        cmbLanguage.addItem(Resources.getString("English"));
        cmbLanguage.addItem(Resources.getString("German"));
        cmbLanguage.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        cmbLanguage.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbLanguageItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.ipadx = 91;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(cmbLanguage, gridBagConstraints);

        cmbMantisse.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        cmbMantisse.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        cmbMantisse.setSelectedItem("8");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 79;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(cmbMantisse, gridBagConstraints);

        jTabbedPane3.addTab(Resources.getString("Configuration"), jPanel1);

        jButton1.setText(Resources.getString("Save"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 346, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        AppConfig config = HxCalc.getConfig();
        config.delete();
        if (cmbLanguage.getSelectedIndex() == 0) {
            config.setProperty("user.country", "US");
            config.setProperty("user.language", "en");
        } else {
            config.setProperty("user.language", "de");
            config.setProperty("user.country", "DE");
        }
        config.setProperty("defultmantisse", (String) cmbMantisse.getSelectedItem());
        if (chExtended.isSelected()) {
            config.setProperty("defaultendnotaion", "Yes");
        } else {
            config.setProperty("defaultendnotaion", "No");
        }
        //config.setProperty("defultmtemp", (String) cmbTemp.getSelectedItem());
        //config.setProperty("defultenthalphy", (String) cmbEnth.getSelectedItem());
        //config.setProperty("defultdensity", (String) cmbDensity.getSelectedItem());
       // config.setProperty("defultpressure", (String) cmbPre.getSelectedItem());
        config.delete();
        try {
            config.save();
            logger.info("Save configuration file");

        } catch (Exception e) {
        }
        
        parent.dispose();
           // HxCalc.getInstance().exit();
       
       
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
               // SwingUtilities.updateComponentTreeUI(parent);
                //parent.getContentPane().update(null);

                try {
                    HxCalc.HCalcMain();
                } catch (Exception e) {
                }

            }
        });


    }//GEN-LAST:event_jButton1ActionPerformed

    private void cmbLanguageItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbLanguageItemStateChanged

        int i = cmbLanguage.getSelectedIndex();
        if (i == 0) {
            Locale.setDefault(new Locale("en", "US"));
        } else {
            Locale.setDefault(new Locale("en", "US"));
        }

        Resources.clearCache();
        Resources.resourceInitialize();
        

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                //SwingUtilities.updateComponentTreeUI();
                //parent.getContentPane().update(null);

                /*
                 * try { HxCalc.HCalcMain(new String[]{"test"}); } catch
                 * (Exception e) {
                }
                 */
            }
        });



        //SwingUtilities.updateComponentTreeUI(parent);


        // TODO add your handling code here:
    }//GEN-LAST:event_cmbLanguageItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chExtended;
    private javax.swing.JComboBox cmbLanguage;
    private javax.swing.JComboBox cmbMantisse;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane3;
    // End of variables declaration//GEN-END:variables
}
